package com.xsp.eduservice.controller;


import com.xsp.eduservice.entity.EduChapter;
import com.xsp.eduservice.entity.chapter.ChapterVo;
import com.xsp.eduservice.service.EduChapterService;
import com.xsp.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-02-11
 */
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    @Autowired
    EduChapterService eduChapterService;

    //通过课程id查询查询课程大纲列表(章节和小节)
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> chapterVos = eduChapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("chapterVos",chapterVos);
    }

    //添加章节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    //根据章节id查询章节信息
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
        EduChapter chapterInfo = eduChapterService.getById(chapterId);
        return R.ok().data("chapterInfo",chapterInfo);
    }

    //修改章节
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    //删除章节(如果章节下有小节，则不能删除)
    @DeleteMapping("deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        boolean b = eduChapterService.deleteChapter(chapterId);
        //删除成功返回ok，删除失败返回error
        return b?R.ok():R.error();
    }


}

